package cn.py.boot_lua.util;

import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;

@Component
public class LuaScriptLoader {

    private final ResourceLoader resourceLoader;
    @Getter
    private String seckillScript;

    public LuaScriptLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @PostConstruct
    public void loadScripts() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:lua/seckill.lua");
        seckillScript = new String(Files.readAllBytes(resource.getFile().toPath()));
    }

}
